﻿Imports System.Management
Imports System.Text

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel

Public Class RAMonitoring

    Public Enum showSize
        kiloBytes
        megaBytes
        gigaBytes
    End Enum

    Public Enum layoutRAM
        All
        Percent
        Size
        Graphic
        PercentSize
        PercentGraphic
        SizeGraphic
    End Enum

    Public Enum graphic
        Graph
        Histogram
    End Enum

    'ATTRIBUTES
    Private DEFAULT_HEIGHT As Integer = 254
    Private DEFAULT_WIDHT As Integer = 500
    Private HALF_WIDHT As Integer = 250
    Private HALF_HEIGHT As Integer = 127

    Private graphic_widht As Integer = 500

    Private layoutSet As layoutRAM = layoutRAM.All

    Private showGraphic As graphic = graphic.Graph

    Private showSizeChoose As showSize = showSize.kiloBytes

    Private monitoring As Boolean = False

    Private ava As Double = My.Computer.Info.AvailablePhysicalMemory
    Private tot As Double = My.Computer.Info.TotalPhysicalMemory
    Private used As Double = tot - ava

    Dim list As New ArrayList

    Dim memoryBit As Bitmap = New Bitmap(graphic_widht, 100)
    Dim graf As Graphics = Graphics.FromImage(memoryBit)
    Dim pencil As New Pen(Color.White, 2)

    Dim startX As Integer = graphic_widht
    Dim startY As Integer = 99 - ((used / tot) * 100)
    Dim endY As Integer = 1

    Dim info As Management.ManagementObject
    Dim processor As New Management.ManagementObjectSearcher("SELECT * FROM Win32_Processor")
    Dim mother As New Management.ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard")



    '----------------------------------------------------------------------------------------------------------'

    Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        UpdateMemory()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        UpdateMemory()
        If showGraphic = graphic.Graph Then
            grapik()
        Else
            grapikHisto()
        End If
    End Sub

    Private Sub UpdateMemory()
        ava = My.Computer.Info.AvailablePhysicalMemory
        used = tot - ava

        lblTotal.Text = MemorySizeFormat(tot)
        lblAva.Text = MemorySizeFormat(ava)
        lblUsed.Text = MemorySizeFormat(used)
        lblPercent.Text = CInt((used / tot) * 100).ToString + "%"
    End Sub

    Private Function MemorySizeFormat(ByVal size As Double) As String
        Dim memSize As String = CStr(size)
        If showSizeChoose = showSize.kiloBytes Then
            memSize = Format(CInt(CDec(memSize) / 1024), "###,###,###,##0 KB")
        ElseIf showSizeChoose = showSize.megaBytes Then
            memSize = Format(CInt(CDec(memSize) / 1024 / 1024), "###,###,##0 MB")
        ElseIf showSizeChoose = showSize.gigaBytes Then
            memSize = Format(CInt(CDec(memSize) / 1024 / 1024 / 1024), "#,###.00 GB")
        End If

        Return memSize
    End Function

    Private Sub grapik()
        Dim percentage As Integer
        graf.Clear(pictbox.BackColor)
        graf.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        percentage = 100 - ((used / tot) * 100)
        endY = Convert.ToInt32(percentage)
        startX = graphic_widht
        list.Add(endY)
        For i = list.Count To 0 Step -1
            If i <= 1 Then
                Continue For
            End If
            'Label3.Text = arrX.Capacity
            graf.DrawLine(pencil, startX, list.Item(i - 1), startX - 2, list.Item(i - 2))
            startX -= 2
            If list.Count > 251 Then
                list.RemoveAt(1)
            End If
        Next
        pictbox.Image = memoryBit
    End Sub

    Private Sub grapikHisto()
        Dim percentage As Integer
        graf.Clear(pictbox.BackColor)
        graf.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        percentage = 100 - ((used / tot) * 100)
        endY = Convert.ToInt32(percentage)
        startX = graphic_widht
        list.Add(endY)
        For i = list.Count To 0 Step -1
            If i <= 1 Then
                Continue For
            End If
            'Label3.Text = arrX.Capacity
            graf.DrawLine(pencil, startX, list.Item(i - 1), startX, 100)
            startX -= 1
            If list.Count > 251 Then
                list.RemoveAt(1)
            End If
        Next
        pictbox.Image = memoryBit
    End Sub




    'PROPERTIES
    Public Overrides Property BackColor As Color
        Get
            Return MyBase.BackColor
        End Get
        Set(value As Color)
            MyBase.BackgroundImage = Nothing
            MyBase.BackColor = value

            lblPercent.BackColor = value
            lblTotal.BackColor = value
            lblAva.BackColor = value
            lblUsed.BackColor = value
            titleAva.BackColor = value
            titleTot.BackColor = value
            titleUsed.BackColor = value
        End Set
    End Property

    Public Overrides Property ForeColor As Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(value As Color)
            MyBase.ForeColor = value
            lblPercent.ForeColor = value
            lblTotal.ForeColor = value
            lblAva.ForeColor = value
            lblUsed.ForeColor = value
            titleAva.ForeColor = value
            titleTot.ForeColor = value
            titleUsed.ForeColor = value
            pencil.Color = value
        End Set
    End Property


    Public Property SizeType As showSize

        Get
            Return showSizeChoose
        End Get
        Set(value As showSize)
            showSizeChoose = value
            UpdateMemory()
        End Set
    End Property

    Public Property GraphicType As graphic

        Get
            Return showGraphic
        End Get
        Set(value As graphic)
            showGraphic = value
        End Set
    End Property

    Public Property SelectLayoutRAM As layoutRAM

        Get
            Return layoutSet
        End Get
        Set(value As layoutRAM)
            layoutSet = value
            If value = layoutRAM.All Then
                Me.Height = DEFAULT_HEIGHT
                Me.Width = DEFAULT_WIDHT
                Panel1.Visible = True
                Panel2.Visible = True
                pictbox.Visible = True

                Panel1.Location = New Point(0, 0)
                Panel2.Location = New Point(250, 0)
                pictbox.Location = New Point(0, 141)
                graphic_widht = DEFAULT_WIDHT
                pictbox.Width = DEFAULT_WIDHT

            ElseIf value = layoutRAM.PercentSize Then
                Me.Height = HALF_HEIGHT
                Me.Width = DEFAULT_WIDHT
                pictbox.Visible = False

                Panel1.Visible = True
                Panel2.Visible = True
                Panel1.Location = New Point(0, 0)
                Panel2.Location = New Point(250, 0)

            ElseIf value = layoutRAM.PercentGraphic Then
                Me.Height = HALF_HEIGHT
                Me.Width = DEFAULT_WIDHT
                Panel2.Visible = False

                Panel1.Visible = True
                pictbox.Visible = True
                Panel1.Location = New Point(0, 0)
                pictbox.Location = New Point(250, 14)
                graphic_widht = HALF_WIDHT
                pictbox.Width = HALF_WIDHT

            ElseIf value = layoutRAM.SizeGraphic Then
                Me.Height = HALF_HEIGHT
                Me.Width = DEFAULT_WIDHT
                Panel1.Visible = False

                Panel2.Visible = True
                pictbox.Visible = True
                Panel2.Location = New Point(0, 0)
                pictbox.Location = New Point(250, 14)
                graphic_widht = HALF_WIDHT
                pictbox.Width = HALF_WIDHT

            ElseIf value = layoutRAM.Percent Then
                Me.Width = HALF_WIDHT
                Me.Height = HALF_HEIGHT
                pictbox.Visible = False
                Panel2.Visible = False

                Panel1.Visible = True
                Panel1.Location = New Point(0, 0)

            ElseIf value = layoutRAM.Size Then
                Me.Width = HALF_WIDHT
                Me.Height = HALF_HEIGHT
                pictbox.Visible = False
                Panel1.Visible = False

                Panel2.Visible = True
                Panel2.Location = New Point(0, 0)

            ElseIf value = layoutRAM.Graphic Then
                Me.Height = HALF_HEIGHT
                Me.Width = DEFAULT_WIDHT
                Panel1.Visible = False
                Panel2.Visible = False

                pictbox.Visible = True
                pictbox.Location = New Point(0, 14)
                graphic_widht = DEFAULT_WIDHT
                pictbox.Width = DEFAULT_WIDHT

            End If
        End Set
    End Property

    Public Property RealTimeMonitoring As Boolean
        Get
            Return monitoring
        End Get
        Set(value As Boolean)
            monitoring = value
            If value Then
                startMonitoring()
            Else
                stopMonitoring()
            End If

        End Set
    End Property

    Public Overrides Property BackgroundImage As Image
        Get
            Return MyBase.BackgroundImage
        End Get
        Set(value As Image)
            'Dim image As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
            ' Dim gr As Graphics = Graphics.FromImage(value)
            'Dim rect As New Rectangle(New Point(0, 0), Me.ClientSize)
            MyBase.BackgroundImage = value

            lblPercent.BackColor = Color.Transparent
            lblTotal.BackColor = Color.Transparent
            lblAva.BackColor = Color.Transparent
            lblUsed.BackColor = Color.Transparent
            titleAva.BackColor = Color.Transparent
            titleTot.BackColor = Color.Transparent
            titleUsed.BackColor = Color.Transparent
            Panel1.BackColor = Color.Transparent
            Panel2.BackColor = Color.Transparent
            pictbox.BackColor = Color.Transparent
        End Set
    End Property

    'Methods
    Public Sub startMonitoring()
        Timer1.Start()
    End Sub

    Public Sub stopMonitoring()
        Timer1.Stop()
    End Sub

    Public Function getTotalMemory() As Double
        Dim memtot As Double = tot / 1024

        Return memtot
    End Function

    Public Function getAvailableMemory() As Double
        Dim memava As Double = My.Computer.Info.AvailablePhysicalMemory

        memava = memava / 1024

        Return memava
    End Function

    Public Function getUsedMemory() As Double
        Dim memava As Double = My.Computer.Info.AvailablePhysicalMemory
        Dim memUsed As Double = (tot - memava) / 1024

        Return memUsed
    End Function

    Public Function getMemoryPercentage() As Double
        Dim memAva As Double = My.Computer.Info.AvailablePhysicalMemory
        Dim memUsed As Double = tot - memAva
        Return CInt((memUsed / tot) * 100)
    End Function

    Public Function getProcessor() As String
        Dim pros As String = ""

        For Each Me.info In processor.Get()

            pros = info("Name").ToString
        Next
        Return pros
    End Function


    Public Function getPCname() As String
        Return Environment.MachineName.ToString
    End Function

    Public Function getOS() As String
        Return My.Computer.Info.OSFullName
    End Function

    Public Function getMotherboard() As String
        Dim pros As String = Nothing

        For Each Me.info In mother.Get()

            pros = info("Product").ToString
        Next
        Return pros
    End Function
End Class
